<?php if (!defined('BASE')) {die('No direct file access.');}

//********************************
//	File:		submind.php
//	Author:		Philip Pryce
//	Date:		20th July 2008
//	Comments:	The belly of the beast, this contains the Submind class that kicks everything off.
//********************************

// ---
// Lets include some important constants so everything works nice.
// ---
include(SYS . 'submind/constants.php');


// ---
// Submind Class, where the fireworks are really made!
// ---
class Submind {
	
	// -------
	// lets ready a private var to keep everything referenced in.
	// -------
	private static $instance;
	
	// lets make sure we dont load two of the same helpers, nasty error if we do...
	var $loadedHelpers = array();
	
	// -------
	// when Submind is first created lets do some important stuff
	// -------
	function __construct () {
		
		// Make a reference of the whole of Submind so we can use it inside other things..
		self::$instance =& $this;
		
		// Load some required core librarys
		$this->loadLibrary('config');
		$this->loadLibrary('controller');
		
		// Start the auto load sequence!
		$this->config->load('autoload');
		// cache this thank you please.
		$autoload = $this->config->get('autoload');
		// lets load these libraries then...
		foreach ($autoload['libraries'] as $library) {
			$this->loadLibrary($library);
		}
		// now for the models...
		foreach ($autoload['models'] as $model) {
			$this->loadModel($model);
		}
		// and finally the helpers.
		foreach ($autoload['helpers'] as $helper) {
			$this->loadHelper($helper);
		}
		// lets clean up a bit.
		unset($autoload);
		
		
		// lets kick it all off then!
		$this->loadLibrary('request');
		
	}
	
	// -------
	// make getting a reference of Submind a little easier.
	// -------
	static function &getInstance () {
		return self::$instance;
	}
	
	// -------
	// this function will check if the library is loaded, if not
	// it will check if they exist, then load them up.
	// -------
	function loadLibrary ($name) {
		
		if (isset($this->{$name})) {
			return $this->{$name};
		}
		
		if (file_exists(SYS . 'libraries/' . $name . '.php')) {
			require_once(SYS . 'libraries/' . $name . '.php');
		}
		
		$classname = ucfirst($name);
		
		if (!class_exists($classname)) {
			displayError(ERROR_FATAL, 'Submind->loadLibrary', 'Class "' . $classname . '" doesnt exist inside of "' . SYS . 'libraries/' . $name . '.php".');
		}
		
		$this->{$name} = new $classname();
		return $this->{$name};
	}
	
	// -------
	// this function will check if the model is loaded, if not
	// it will check if they exist, then load them up.
	// -------
	function loadModel ($name) {
		
		$this->loadLibrary('models');
		
		if (isset($this->{$name})) {
			return $this->{$name};
		}
		
		if (file_exists(APP . 'models/' . $name . '.php')) {
			require_once(APP . 'models/' . $name . '.php');
		}
		
		$classname = ucfirst($name);
		
		if (!class_exists($classname)) {
			displayError(ERROR_FATAL, 'Submind->loadModel', 'Class "' . $classname . '" doesnt exist inside of "' . APP . 'models/' . $name . '.php".');
		}
		
		$this->{$name} = new $classname();
		return $this->{$name};
	}
	
	// -------
	// this function will look for helpers in _both_ the APP and SYS folder
	// then atempt to load them.
	// -------
	function loadHelper ($name) {
		
		// You've loaded this helper, you tool.
		// Oh well we'll save you the errors...
		if (array_key_exists($name, $this->loadedHelpers)) {
			return;
		}
		
		// Check if the helper exists in the APP directory first incase you've overwritten our
		// default helpers with your own.
		if (file_exists(APP . 'helpers/' . $name . '_helper.php')) {
			// Load her up
			include_once(APP . 'helpers/' . $name . '_helper.php');
		}
		else {
			// Right, lets see if it exists in the SYS directory then...
			if (file_exists(SYS . 'helpers/' . $name . '_helper.php')) {
				// Load her up...
				include_once(SYS . 'helpers/' . $name . '_helper.php');
			}
			else {
				// No luck..
				displayError(ERROR_FATAL, 'Submind->loadHelper', 'Helper "' . $name . '" doesnt exist inside of "' . SYS . 'helpers/' . $name . '_helper.php".');
			}
		}
		// add it to the list
		$this->loadedHelpers[$name] = true;
		// and now we return
		return true;
		
	}
	
	// -------
	// unset everything we've worked with to save a little memory.
	// -------
	function __destruct () {
		
		foreach(get_object_vars($this) as $key => $value) {
			// destroy all the objects in here when we die.
			unset($key);
		}
		
	}
	
}

// -------
// Here we load some framework wide functions.
// -------

// -------
// Simply makes getting a reference of the Submind object a little easier on the eyes.
// -------
function &getInstance () {
	return Submind::getInstance();
}

// -------
// this allows for redirections
// -------
function redirect ($path) {
	header("Location: " . WEBURL . '?' . $path);
	die();
}

// -------
// displays a short but sweet error message, hopefully making things nicer soon.
// -------
function displayError($level, $from, $message) {
	
	$return = '[<b>' . $level . '</b>] - ' . $from . '() threw this error message: - <br />' . $message;
	
	// if this is a fatal error, lets us die rather
	// than be caught with our pants down...
	if ($level == 1) {
		die($return);
	}
	
	echo $return;
	
	return;
	
}

?>